/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import common.Common;
import entities.Attendance;
import java.util.Date;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Sony
 */
public class AttendanceTableModel extends AbstractTableModel {

    public final static int LABOR_ID = 0;
    public final static int DAY = 1;
    public final static int DAILY_ATTENDANCE = 2;
    public final static int SHIFT_ID = 3;
    public final static int EXTRA_SHIFT = 4;
    public final static int SALARY_DAILY = 5;
    private String[] columnNames = {"Labor ID", "Day", "Daily Attendance", "Shift ID", "Extra Shift", "Salary Daily"};
    private Vector<Attendance> data;
    Common com = new Common();

    public AttendanceTableModel(Vector<Attendance> data) {
        this.data = data;
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Attendance attendance = (Attendance) data.get(rowIndex);
        switch (columnIndex) {
            case LABOR_ID:
                return attendance.getLaborID();
            case DAY:
                return attendance.getDay();
            case DAILY_ATTENDANCE:
                return attendance.isDailyAttendance();
            case SHIFT_ID:
                return attendance.getShiftID();
            case EXTRA_SHIFT:
                return attendance.getExtraShift();
            case SALARY_DAILY:
                return attendance.getSalaryDaily();
            default:
                return new Object();
        }
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        Attendance attendance = (Attendance) data.get(rowIndex);
        switch (columnIndex) {
            case LABOR_ID:
                attendance.setLaborID(String.valueOf(aValue));
                break;
            case DAY:
                attendance.setDay(com.convertStringToDate(String.valueOf(aValue)));
                break;
            case DAILY_ATTENDANCE:
                attendance.setDailyAttendance(Boolean.valueOf(String.valueOf(aValue)));
                break;
            case SHIFT_ID:
                attendance.setShiftID(String.valueOf(aValue));
                break;
            case EXTRA_SHIFT:
                attendance.setExtraShift(Integer.valueOf(String.valueOf(aValue)));
                break;
            case SALARY_DAILY:
                attendance.setSalaryDaily(Float.valueOf(String.valueOf(aValue)));
                break;
            default:
                System.out.println("INVALID INDEX");
        }
        fireTableCellUpdated(rowIndex, columnIndex);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case LABOR_ID:
                return String.class;
            case DAY:
                return Date.class;
            case DAILY_ATTENDANCE:
                return Boolean.class;
            case SHIFT_ID:
                return String.class;
            case EXTRA_SHIFT:
                return Integer.class;
            case SALARY_DAILY:
                return Float.class;
            default:
                return Object.class;
        }
    }

    public void setData(Vector<Attendance> data) {
        this.data = data;
    }
}
